package com.chisheng.controller;

import com.chisheng.pojo.Page;
import com.chisheng.pojo.Student;
import com.chisheng.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @author chisheng
 * @since 2021-11-08
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    /**
     * 学生 BLL
     */
    private final StudentService studentService;

    @Autowired
    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    @PostMapping("/add")
    public String add(Student student, RedirectAttributes ra) {
        boolean result = studentService.add(student);
        ra.addFlashAttribute("status", result);
        ra.addFlashAttribute("message", result ? "添加成功" : "添加失败");
        return "redirect:/student/page/1/12";

    }

    @GetMapping("/remove/{id}")
    public String remove(@PathVariable Integer id, RedirectAttributes ra) {
        boolean result = studentService.remove(id);
        ra.addFlashAttribute("status", result);
        ra.addFlashAttribute("message", result ? "移除成功" : "移除失败");
        return "redirect:/student/page/1/12";
    }

    @PostMapping("/update")
    public String update(Student student, RedirectAttributes ra) {
        boolean result = studentService.update(student);
        ra.addFlashAttribute("status", result);
        ra.addFlashAttribute("message", result ? "更新成功" : "更新失败");
        return "redirect:/student/page/1/12";
    }

    @GetMapping("/edit/{id}")
    public ModelAndView get(@PathVariable Integer id, ModelAndView modelAndView) {
        Student student = studentService.query(id);
        modelAndView.setViewName("/edit.jsp");

        if (student != null) {
            modelAndView.addObject("status", true);
            modelAndView.addObject("student", student);
            modelAndView.addObject("message", "数据回显成功");
        } else {
            modelAndView.addObject("status", false);
            modelAndView.addObject("message", "数据回显失败");
        }

        return modelAndView;
    }

    @GetMapping("/page/{current}/{size}")
    public ModelAndView getPage(@PathVariable Integer current, @PathVariable Integer size, ModelAndView modelAndView,
                                ModelMap modelMap) {
        Page<Student> page = studentService.queryPage(current, size);
        modelAndView.setViewName("/list.jsp");

        if (page != null) {
            modelAndView.addObject("status", true);
            modelAndView.addObject("page", page);
            modelAndView.addObject("message", String.format("欢迎访问本站，本站共收录学生信息 %d 条，共计 %d 页",
                    page.getTotal(), page.getTotal() / page.getSize()));
        } else {
            modelAndView.addObject("status", false);
            modelAndView.addObject("message", "数据回显失败");
        }

        modelAndView.addAllObjects(modelMap);
        return modelAndView;
    }

}
